module GcalHelper
  
     
  #final and parse hash with events
  $event_hash = {}
  #an array with desciptions for a single event
  @desc_attributes = []
  #backup hash with description for a single event, This is the one store as a value in $event_hash
  $description_hash = {}
  #The next 3 are used because the language in the response is German
  $when = "Wann:"
  $where = "Wer:"
  $event_description = "Terminbeschreibung:"
  #the way to split the whole description
  $br = "<br />"
  #A simple counter used to store with a different number every event
  $event_counter = 0
  
  #Notice that the only method used outside this class is the event_parser.
  #The mentioned method is used in the users_controller
  
  
  #We start parsing the events from the response.
  #event_array is passed from the controller and is an array of strings.
   def event_parser(titlearray,descarray) 
      for counter in 0..descarray.length-1
#        puts "________________________"
#        puts titlearray[counter]
#        puts descarray[counter]
#        puts "________________________"
        @desc_attributes = descarray[counter].split($br)
        @desc_attributes = desc_attributes_parser()
        filling_event_hash(titlearray[counter])
      end
#      print_test_for_the_event_hash
   # readxml
    return $event_hash
  end
 
  #We look and store in a local array the description needed.
  #For this we created the variables $when , $where and $event_description
  def desc_attributes_parser()
    parsed_array = []
    counter = 0
    @desc_attributes.each do |x| 
       if (x.include?($when) || x.include?($where) || x.include?($event_description))
         parsed_array[counter] = x
         counter+=1
       end
   end
   return parsed_array
  
  end
  
  
  #We start adding events to the parse and final event hash
  # The information comes form the array already parse with the needed information
  def filling_event_hash(title)
    $description_hash.merge!("Title"=>title)
    @desc_attributes.each do |x|
      if (x.include?($when))
        a = x.split($when)
        #If the current_time_outside_range return true then that means 
        #that the time for the current event is outside the range. Hence return!
        $description_hash.merge!({"When"=>a[1]})
      elsif (x.include?($where)) 
        a = x.split($where)
        $description_hash.merge!({"Where"=>a[1]})
      else (x.include?($event_description)) 
        a = x.split($event_description)
        $description_hash.merge!({"Description"=>a[1]})    
      end
    end
    # Merge the description hash to the final event hash!
    $event_hash.merge!({"Event "+$event_counter.to_s => $description_hash})
    $event_counter += 1
    #If we clear the hash has a different result. To be studied!
    #This way works
    $description_hash = {}
  end
  
  #We could have use a Command Pattern to get rid of the case but we are in the initial phase of the project.
  def translate_month_to_number(month)
    case month
      when "Mai"then return "5"
    end
  end
  
 
  #Just a debugging method to check the information inside the event_hash
  def print_test_for_the_event_hash
      $event_hash.each do |k , v|
        puts "***** "+k+" *****"
        v.each do |q,w| 
          puts q 
          puts w
        end  
        puts "__________"
      end
  end
  
end
